package gui;

import java.awt.BorderLayout;
import java.awt.Checkbox;
import java.awt.Dimension;
import java.awt.HeadlessException;
import java.awt.LayoutManager;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.JToolBar;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;

import world.Map;


public class MainFrame extends JFrame implements ActionListener {
	
	final int MIN_WIDTH = 502;				// window size, width
	final int MIN_HEIGHT = 500;				// window size, height
	private final int X = 100;
	private final int Y = 50;
	private LayoutManager layoutManager;
	private GamePanel gamePanel;
	
	private static final int MIN_FIELD_SIZE = 0;
	public static final int DEF_FIELD_SIZE = 10;
	private static final int MAX_FIELD_SIZE = 30;
	
	private int fieldSize = DEF_FIELD_SIZE;
	
	JSpinner sizeSpinner;
	
	private static final String START = "Start >";
	private static final String PAUSE = "Pauza ||";
	private static final String NEXT_STEP = "Krok |>";
	
	private Checkbox expandTree;

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public MainFrame() throws HeadlessException {
		this.setLocation(new Point(X, Y));
		this.setMinimumSize(new Dimension(MIN_WIDTH,MIN_HEIGHT)); 	// sets minimum frame size
		this.setTitle("MSI Wumpus");								//set frame title
		this.fieldSize = Main.getInstance().getMap().getSize();
		layoutManager=new BorderLayout(); 
		setLayout(layoutManager);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	
		gamePanel = new GamePanel(this);
		getContentPane().add(gamePanel,BorderLayout.CENTER); // in the center of the frame
		
		
		// toolbar
	    JToolBar toolBar = new JToolBar("Still draggable");
	    // buttons
	    JButton button = null;
	    button = makeNavigationButton(START, "Kontynuuje symulację", "start");
	    toolBar.add(button);
	    button = makeNavigationButton(PAUSE, "Wstrzymuje symulację", "pauza");
	    toolBar.add(button);
	    button = makeNavigationButton(NEXT_STEP, "Przechodzi do następnego kroku", "krok");
	    toolBar.add(button);
	    
	    toolBar.addSeparator(new Dimension(50, 10));
	    
	    expandTree = new Checkbox("rozwiń drzewo", false);
	    toolBar.add(expandTree);
	    
	    toolBar.addSeparator(new Dimension(100, 10));
	    
//	    // Separator
//	    toolBar.addSeparator(new Dimension(50, 10));
//	    // Label
//	    JLabel label = new JLabel("Rozmiar planszy: ");
//	    toolBar.add(label);
//	    // Spinner
//	    SpinnerModel model =
//	        new SpinnerNumberModel(fieldSize,	//initial value
//	                               MIN_FIELD_SIZE,	//min
//	                               MAX_FIELD_SIZE,	//max
//	                               1);	//step
//	    sizeSpinner = new JSpinner(model);
//	    toolBar.add(sizeSpinner);
//	    // Separator
//	    toolBar.addSeparator(new Dimension(500, 10));
	    
	    getContentPane().add(toolBar, BorderLayout.PAGE_START);
	}
	
	
	public int getFieldSize() {
		return fieldSize;
	}


	protected JButton makeNavigationButton( String actionCommand, String toolTipText, String altText) {
		//Create and initialize the button.
		JButton button = new JButton(actionCommand);
		button.setActionCommand(actionCommand);
		button.setToolTipText(toolTipText);
		button.addActionListener(this);
		
	    return button;
	}


	@Override
	public void actionPerformed(ActionEvent e) {
	    String cmd = e.getActionCommand();

	    // Handle each button.
	    if (START.equals(cmd)) {				//first button clicked
	    	Main.getInstance().continueSimulation();
	    } else if (PAUSE.equals(cmd)) { 			// second button clicked
	    	Main.getInstance().pauseSimulation();
	    } else if (NEXT_STEP.equals(cmd)) {			// third button
	    	Main.getInstance().nextStep();
	    }
	}
	
	
	public void reload() {
		gamePanel.repaint();
	}
	
	
	public boolean getExpandTreeState() {
		return expandTree.getState();
	}

	
}